package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.ContactManagement;
import com.gxa.mapper.ContactModeMapper;
import com.gxa.service.ContactModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class ContactModeServiceImpl implements ContactModeService {

    @Autowired
    private ContactModeMapper contactModeMapper;

    /**
     * 添加接触方式
     * @param contactManagement
     * @return
     */
    @Override
    @Transactional
    public R addContactMode(ContactManagement contactManagement) {
        R r = new R();
        try {
            //根据接触方式名称查询接触方式信息
            ContactManagement selectedContactManagement = contactModeMapper.selectContactModeByName(contactManagement.getContactMode());
            if(selectedContactManagement == null){
                //要添加的接触方式不存在，进行添加
                contactModeMapper.insertContactMode(contactManagement);
                r.setCode(200);
                r.setMsg("添加成功！");
            }else {
                r.setCode(1001);
                r.setMsg("添加失败，要添加的接触方式已存在！");
            }
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,添加失败,请联系管理员");
        }
        return r;
    }

    /**
     * 根据ID删除接触方式
     * @param id
     * @return
     */
    @Override
    public R deleteContactMode(int id) {
        R r = new R();
        try {
            contactModeMapper.deleteContactMode(id);
            r.setCode(200);
            r.setMsg("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,删除失败,请联系管理员");
        }
        return r;
    }

    /**
     * 分页查询所有的接触方式
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryAllContactModesByPage(int currentPage, int pageSize) {
        R r = new R();
        try {
            Page<ContactManagement> page = PageHelper.startPage(currentPage,pageSize);
            List<ContactManagement> contactManagementList = contactModeMapper.selectAllContactModes();
            int count = (int)page.getTotal();
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setCount(count);
            r.setData(contactManagementList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }

    /**
     * 修改接触方式
     * @param contactManagement
     * @return
     */
    @Override
    @Transactional
    public R updateContactMode(ContactManagement contactManagement) {
        R r = new R();
        try {
            //先删除该接触方式
            contactModeMapper.deleteContactMode(contactManagement.getId());
            //再添加修改后的接触方式
            contactModeMapper.insertContactModeWithFixedId(contactManagement);
            r.setCode(200);
            r.setMsg("修改成功！");
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,修改失败,请联系管理员");
        }
        return r;
    }
}
